package org.jz.gae.redbook.spb.client.slate.location;

import java.util.ArrayList;
import java.util.List;

import org.jz.gae.redbook.spb.client.toolkit.EntityViewGrid;
import org.jz.gae.redbook.spb.client.toolkit.EntityViewGrid.ColumnValueDisplayer;
import org.jz.gae.redbook.spb.client.toolkit.Slate;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;
import org.jz.gae.redbook.spb.shared.LocationEntityView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LocationListPlate extends Composite
{
	  private final Button refreshButton = new Button("Обновить");
	  private final Button addButton = new Button("Добавить");
	  private final Button openButton = new Button("Открыть");
	  private final FlexTable contentTable;
	  private EntityViewGrid<LocationEntityView> locationListGrid;
	private Slate slate; 
	  
	  public LocationListPlate() 
	  {
		  SimplePanel contentTableDecorator = new SimplePanel();
	    initWidget(contentTableDecorator);
	    contentTableDecorator.setWidth("100%");

	    contentTable = new FlexTable();
	    contentTable.setWidth("100%");
	    //contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListContainer");
	    //contentTable.getCellFormatter().setWidth(0, 0, "100%");
	    contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);
	    
	    // Create the menu
	    //
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setBorderWidth(0);
	    hPanel.setSpacing(0);
	    hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
	    hPanel.add(addButton);
	    //contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListMenu");
	    contentTable.setWidget(0, 0, hPanel);
	    
	    // Create the contacts list
	    //
	    locationListGrid = new EntityViewGrid<LocationEntityView>();
	    locationListGrid.setWidth("100%");
	    locationListGrid.addButton(refreshButton);
	    locationListGrid.addButton(addButton);
	    locationListGrid.addButton(openButton);
	    
	    locationListGrid.addGridColumn("Район", "180px", new ColumnValueDisplayer<LocationEntityView>(){
	    	@Override
	    	public String displayColumnValue(LocationEntityView _EntityView)
	    	{
	    		return _EntityView.getLocationDistrictPresentation();
	    	}
	    },
	    new TextColumn<LocationEntityView>(){
	    	@Override
	    	public String getValue(LocationEntityView _EntityView)
	    	{
	    		return _EntityView.getLocationDistrictPresentation();
	    	}
	    });

	    locationListGrid.addGridColumn("Вид", "180px", new ColumnValueDisplayer<LocationEntityView>(){
	    	@Override
	    	public String displayColumnValue(LocationEntityView _EntityView)
	    	{
	    		return _EntityView.getLocationSpeciesPresentation();
	    	}
	    },
	    new TextColumn<LocationEntityView>(){
	    	@Override
	    	public String getValue(LocationEntityView _EntityView)
	    	{
	    		return _EntityView.getLocationSpeciesPresentation();
	    	}
	    });

	    locationListGrid.addGridColumn("Рабочее наменование", null, new ColumnValueDisplayer<LocationEntityView>(){
	    	@Override
	    	public String displayColumnValue(LocationEntityView _EntityView)
	    	{
	    		return _EntityView.getLocationWorkName();
	    	}
	    },
	    new TextColumn<LocationEntityView>(){
	    	@Override
	    	public String getValue(LocationEntityView _EntityView)
	    	{
	    		return _EntityView.getLocationWorkName();
	    	}
	    });
	    contentTable.setWidget(1, 0, locationListGrid);
	    
	    contentTableDecorator.add(contentTable);
	    
	    refreshButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	fetchLocationDetails();
	        }
	      });
	    
	    addButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	appendNewLocation();
 	        }
	      });

	    openButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	openSelectedLocation();
	        }
	      });
	    
	  }
	  
	  public void showLocationList(
			  Slate _Slate)
	  {
		 slate = _Slate;
		 fetchLocationDetails();  
	  }
	  
	  private void fetchLocationDetails() 
	  {
		    slate.getContainerAgent().getRpcService().getLocationDetails(new AsyncCallback<ArrayList<LocationEntityView>>() {
		      public void onSuccess(ArrayList<LocationEntityView> result) 
		      {
		    	  List<LocationEntityView> locationDetails = result;
		          //sortLocationDetails(locationDetails);
		          locationListGrid.setData(locationDetails);
		      }
		      
		      public void onFailure(Throwable caught) {
		        Window.alert("Error fetching location details");
		      }
		    });
		  }

      private void appendNewLocation()
      {
          LocationAppendPlate appendPlate = new LocationAppendPlate();
          appendPlate.appendEntityView(slate, new ClickHandler(){
             	  @Override
		        public void onClick(ClickEvent event)
		        {
		        	fetchLocationDetails();
		        }
          });
      }
	  
      private void openSelectedLocation()
      {
      	LocationEntityView location = locationListGrid.getSelectedEntityView();
      	if (location != null)
      	{
              String id = location.getLocationKey();
              /**/
              LocationDetailPlate detailsPlate = new LocationDetailPlate();
              detailsPlate.openEntityView(slate, id, null);
      	}
      	else
      	{
              System.err.println("event releasing : no location chosen");  
      	}
      }
}
